<template>
  <el-dialog :title="'会员卡调整'" :close-on-click-modal="false" :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" label-width="100px">
      <el-form-item label="类型">
        <el-select v-model="dataForm.cardType" clearable placeholder="全部" disabled>
          <el-option
            v-for="item in cardTypes"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="开卡公司">
        <span>{{ dataForm.companyName }}</span>
      </el-form-item>
      <el-form-item label="操作店铺" prop="shopId">
        <el-select v-model="dataForm.shopId" clearable placeholder="请选择">
          <el-option
            v-for="item in shops"
            :key="item.id"
            :label="item.shopName"
            :value="item.id"
            :disabled="item.status !== 1"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="会员姓名">
        <span>{{ dataForm.memberName }}</span>
      </el-form-item>
      <el-form-item label="会员电话">
        <span>{{ dataForm.memberMobile }}</span>
      </el-form-item>
      <el-form-item label="会员性别" prop="gender">
        <el-radio-group v-model="dataForm.gender">
          <el-radio label="1">男</el-radio>
          <el-radio label="2">女</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="会员生日" prop="birthday">
        <el-date-picker
          v-model="dataForm.birthday"
          align="right"
          type="date"
          placeholder="选择日期"
          format="yyyy-MM-dd"
          value-format="yyyy-MM-dd"
          :editable="false"
          :picker-options="birthDayPickerOptions"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="实收金额" prop="cardFee">
        <el-input v-model="dataForm.cardFee" placeholder="请输入实收金额"></el-input>
      </el-form-item>
      <!-- 次卡 -->
      <el-form-item label="当前剩余次数" v-if="dataForm.cardType === 1">
        <span>{{ dataForm.cardTimes }}</span>
      </el-form-item>
      <el-form-item label="续次" prop="addCardTimes" v-if="dataForm.cardType === 1">
        <el-row>
          <el-col :span="16">
            <el-input
              v-model="dataForm.addCardTimes"
              placeholder="请输入需要增加的次数"
              type="number"
              min="1"
              :disabled="dataForm.cardTimesType == 0"
            ></el-input>
          </el-col>
          <el-col :span="6" style="margin-left: 5px;">
            <el-checkbox v-model="dataForm.cardTimesType" :true-label="0" :false-label="1">不限次</el-checkbox>
          </el-col>
        </el-row>
      </el-form-item>
      <!-- 充值卡 -->
      <el-form-item label="当前余额" v-if="dataForm.cardType === 2">
        <span>{{ dataForm.cardBalance }}</span>
      </el-form-item>
      <el-form-item label="充值金额" prop="addCardBalance" v-if="dataForm.cardType === 2">
        <el-input v-model="dataForm.addCardBalance" placeholder="请输入卡充值金额"></el-input>
      </el-form-item>
      <!-- 次卡才有失效日期 -->
      <el-form-item label="有效期修改" prop="expireTime" v-if="dataForm.cardType === 1">
        <el-date-picker
          v-model="dataForm.expireTime"
          type="date"
          :picker-options="pickerOptions"
          :editable="false"
          placeholder="选择日期"
          format="yyyy-MM-dd"
          value-format="yyyy-MM-dd"
        ></el-date-picker>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" :disabled="disabled" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { getShopsByCompany } from "@/utils";
export default {
  props: {
    cardTypes: {
      type: Array,
      default: function() {
        return [];
      }
    }
  },
  data() {
    return {
      visible: false,
      disabled: false,
      dataForm: {
        id: null,
        memberId: null,
        companyId: null,
        companyName: null,
        shopId: null,
        cardNo: null,
        memberName: null,
        memberMobile: null,
        gender: null,
        birthday: null,
        cardType: null,
        cardFee: null,
        cardBalance: null,
        cardPoints: null,
        cardTimes: null,
        cardTimesType: null,
        expireTime: null,
        addCardTimes: 0, // 需要增加的次数
        addCardBalance: null // 需要充值的金额
      },
      shops: [],
      pickerOptions: {
        disabledDate: time => {
          // 禁止选择今天及其之前的时间
          let nowDate = new Date();
          return time < nowDate;
        }
      },
      birthDayPickerOptions: {
        disabledDate(time) {
          return time.getTime() > Date.now();
        }
      },
      dataRule: {
        shopId: [
          {
            required: true,
            message: "请选择操作店铺",
            trigger: "blur"
          }
        ],
        cardFee: [
          {
            required: true,
            message: "请输入实收金额",
            trigger: "blur"
          }
        ],
        addCardTimes: [
          {
            required: true,
            message: "请输入要增加的次数",
            trigger: "blur"
          }
        ],
        addCardBalance: [
          {
            required: true,
            message: "请输入要充值的金额",
            trigger: "blur"
          }
        ],
        expireTime: [
          {
            required: true,
            message: "请选择有效期",
            trigger: "blur"
          }
        ]
      }
    };
  },
  methods: {
    init(id) {
      this.dataForm = {
        id: id,
        memberId: null,
        companyId: null,
        companyName: null,
        shopId: null,
        cardNo: null,
        memberName: null,
        memberMobile: null,
        gender: null,
        birthday: null,
        cardType: null,
        cardFee: null,
        cardBalance: null,
        cardPoints: null,
        cardTimes: null,
        expireTime: null,
        cardTimesType: null,
        addCardTimes: 0, // 需要增加的次数
        addCardBalance: null // 需要充值的金额
      };
      this.disabled = false;
      if (id) {
        const loading = this.$loading({
          lock: true,
          text: '加载中',
          spinner: 'el-icon-loading',
          background: 'rgba(0, 0, 0, 0.7)'
        });
        this.$http({
          url: this.$http.adornUrl(
            `/member/vipCard/detail/${this.dataForm.id}`
          ),
          method: "get",
          params: this.$http.adornParams()
        })
          .then(({ data }) => {
            if (data && data.code === 0) {
              this.dataForm.memberId = data.item.memberId;
              this.dataForm.companyId = data.item.companyId;
              this.dataForm.companyName = data.item.companyName;
              this.dataForm.cardNo = data.item.cardNo;
              this.dataForm.memberName = data.item.memberName;
              this.dataForm.memberMobile = data.item.memberMobile;
              this.dataForm.gender = data.item.gender;
              this.dataForm.birthday = data.item.birthday;
              this.dataForm.cardType = data.item.cardType;
              // this.dataForm.cardFee = data.item.cardFee;
              this.dataForm.cardBalance = data.item.cardBalance;
              this.dataForm.cardPoints = data.item.cardPoints;
              this.dataForm.cardTimes = data.item.cardTimes;
              this.dataForm.cardTimesType = data.item.cardTimesType;
              this.dataForm.expireTime = data.item.expireTime;
            }
          })
          .then(() => {
            loading.close();
            getShopsByCompany(this.dataForm.companyId, res => {
              this.shops = res;
              this.visible = true;
            });
          });
      }
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs["dataForm"].validate(valid => {
        if (valid) {
          this.disabled = true;
          this.$http({
            url: this.$http.adornUrl("/member/vipCard/update"),
            method: "post",
            data: this.$http.adornData({
              id: this.dataForm.id || undefined,
              shopId: this.dataForm.shopId,
              gender: this.dataForm.gender,
              birthday: this.dataForm.birthday,
              cardType: this.dataForm.cardType,
              cardFee: this.dataForm.cardFee,
              cardBalance: this.dataForm.addCardBalance,
              cardTimes: this.dataForm.addCardTimes,
              cardTimesType: this.dataForm.cardTimesType,
              expireTime: this.dataForm.expireTime
            })
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.visible = false;
                  // this.disabled = false;
                  this.$emit("refreshDataList");
                }
              });
            } else {
              this.$message.error(data.msg);
              this.disabled = false;
            }
          }).catch(() => {
            this.disabled = false;
          });
        }
      });
    }
  }
};
</script>
